<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use fast\Http;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Log;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['login', 'jia'];
    protected $noNeedRight = '*';


    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 小程序登录 新用户周卡（周卡已废弃） 调整到 授权手机号时 新用户获取三日卡
     * @ApiMethod (GET)
     * @param string $code code码
     * @param bool $parent 邀请人id非必填
     * @param bool $gymnasium_id 场馆主键gymnasium_id非必填
     */
    public function login()
    {
        if ($this->request->isGet()) {
            $code         = input('code');
            $parent       = input('parent', false);
            $gymnasium_id = input('gymnasium_id', false);

            !$code && $this->error('code不能为空');

            $pay        = get_addon_config('epay');
            $app_id     = $pay['wechat']['app_id'];
            $app_secret = $pay['wechat']['app_secret'];

            $get_result = (new Http)->get("https://api.weixin.qq.com/sns/jscode2session?appid=$app_id&secret=$app_secret&js_code=$code&grant_type=authorization_code");
            // 对JSON格式的字符串进行解码
            $array = json_decode($get_result, true);

            if (!empty($array['openid'])) {
                $find = Db::name('user')->where(['open_id' => $array['openid']])->field('id')->find();

                // 是否新用户
                if ($find) {
                    $is_new = Db::name('extension')->where(['user_id' => $find['id']])->field('id')->find();
                    if ($is_new) {
                        $new = true;
                    } else {
                        $new = false;
                    }
                    $user_id = $find['id'];
                    Db::startTrans();
                    try {
                        Db::name('user')->where(['open_id' => $array['openid']])->update(['session_key' => $array['session_key']]);
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    }
                } else {
                    // 新用户
                    $insert_data = [
                        'open_id'     => $array['openid'],
                        'createtime'  => time(),
                        'updatetime'  => time(),
                        'status'      => 'normal',
                        'session_key' => $array['session_key'],
                    ];

                    Db::startTrans();
                    try {
                        if ($parent) $insert_data['parent_id'] = $parent;
                        // 添加用户信息
                        $user_id = Db::name('user')->insertGetId($insert_data);
//
//                         *********************************************************************************************
//                         先初始化奖励卡信息、Redis
//                        $extension = [
//                            'user_id'       => $user_id,
//                            'type'          => 1,
//                            'validity_time' => time() + 3600 * 24 * 21,
//                            'is_use'        => 1,
//                            'createtime'    => time(),
//                            'updatetime'    => time(),
//                        ];
//                        $redis     = new \Redis();
//                        $redis->pconnect(config('redis.host'), config('redis.port'), config('redis.timeout'));
//
//                         存在上一级 说明是被邀请的 但不一定是通过场馆二维码进入的
//                        if ($parent) {
//                            $extension['parent_id'] = $parent;
//                        }
//                        /**
//                         * 存在 场馆参数说明是通过场馆进入的，判断场馆是否参加推广奖励活动，新用户直接得到该场馆的周卡
//                         * 不存在 则周卡不绑定场馆，该周卡不可用，通过场馆二维码邀请新人后场馆与该周卡绑定
//                         * */
//                        if ($gymnasium_id) {
//                            $find_gymnasium = Db::name('gymnasium')->where(['id' => $gymnasium_id])->field('is_join')->find();
//                            if ($find_gymnasium['is_join'] == 2) {
//                                $extension['gymnasium_id'] = $gymnasium_id;
//                            } else {
//                                $extension['is_use'] = 0;
//                            }
//                        } else {
//                            $extension['is_use'] = 0;
//                        }
//                         新用得到周卡
//                        $extension_id = Db::name('extension')->insertGetId($extension);
//                        $redis->select(6);
//                        $redis->setex($extension_id, 3600 * 24 * 21, '');
//
//                         *********************************************************************************************
//                         老用户奖励次数、是否奖励私教卡、老用户得周卡变为可用状态
//                        /**
//                         * 存在场馆参数说明是通过场馆进入的，存在场馆参数邀请人必须存在
//                         * 不存在 老用户无奖励
//                         * */
//                        if ($extension['is_use'] == 1) {
//                            Db::name('user')->where(['id' => $parent])->setInc('extension_times');
//                            $find_parent = Db::name('user')->where(['id' => $parent])->field('parent_id,extension_times')->find();
//
//                            if ($find_parent) {
//                                $extension['parent_id'] = $find_parent['parent_id'];
//                                $extension['user_id']   = $parent;
//                                $extension['type']      = 2;
//                                $extension['is_use']    = 1;
//
//                                // 老用必得三日卡
//                                if ($find_parent['extension_times'] <= 7) {
//                                    $extension['validity_time'] = time() + 3600 * 24 * 9;
//                                    $extension_id               = Db::name('extension')->insertGetId($extension);
//                                    $redis->select(6);
//                                    $redis->setex($extension_id, 3600 * 24 * 9, '');
//                                }
//                                $extension['type'] = 3;
//                                // 3人一次私教卡
//                                if ($find_parent['extension_times'] == 3) {
//                                    $extension['validity_time'] = time() + 3600 * 24 * 7;
//                                    $extension['end_number']    = 1;
//                                    $extension_id               = Db::name('extension')->insertGetId($extension);
//                                    $redis->select(6);
//                                    $redis->setex($extension_id, 3600 * 24 * 7, '');
//                                }
//                                // 7人3次私教卡
//                                if ($find_parent['extension_times'] == 7) {
//                                    $extension['validity_time'] = time() + 3600 * 24 * 21;
//                                    $extension['end_number']    = 3;
//                                    $extension_id               = Db::name('extension')->insertGetId($extension);
//                                    $redis->select(6);
//                                    $redis->setex($extension_id, 3600 * 24 * 21, '');
//                                }
//
//                                // 改变老用户周卡
//                                $find_parent_extension = Db::name('extension')->where(['user_id' => $parent, 'type' => 1, 'is_use' => 0])->find();
//                                if ($find_parent_extension) {
//                                    Db::name('extension')->where(['user_id' => $parent, 'type' => 1, 'is_use' => 0])->update(['is_use' => 1, 'gymnasium_id' => $gymnasium_id]);
//                                }
//                            }
//                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    }
                }
                $this->auth->direct($user_id) ? $this->success('登录成功', $this->auth->getUserinfo($new)) : $this->error($this->auth->getError());

            } else {
                $this->error('登陆失败，请检查小程序配置');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 微信授权 【更新头像、昵称】
     * @ApiMethod (GET)
     * @param string $nickname 昵称
     * @param string $avatar 头像地址
     */
    public function auth()
    {
        if ($this->request->isGet()) {
            $nickname = input('nickname');
            $avatar   = input('avatar');

            !$nickname && $this->error('请传入nickname');
            !$avatar && $this->error('请传入avatar');

            $data = [
                'nickname'   => $nickname,
                'avatar'     => $avatar,
                'updatetime' => time(),
            ];
            try {
                Db::name('user')->where(['id' => $this->auth->id])->update($data);
            } catch (PDOException $e) {
                $this->error($e->getMessage());
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('更新用户信息成功');
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 微信授权 【更新手机号】
     * @ApiMethod (GET)
     * @param $encryptedData string 加密的信息
     * @param $iv string iv 向量
     */
    public function authPhone()
    {
        if ($this->request->isGet()) {

            $encryptedData = input('encryptedData');
            $iv            = input('iv');

            !$encryptedData && $this->error('请传入encryptedData');
            !$iv && $this->error('请传入iv');

            $find = Db::name('user')->where(['id' => $this->auth->id])->field('')->find();

            // 解密数据
            $result = json_decode(openssl_decrypt(base64_decode($encryptedData), "AES-128-CBC", base64_decode($find['session_key']), 1, base64_decode($iv)));

            if ($result) {
                try {
                    Db::name('user')->where(['id' => $this->auth->id])->update(['mobile' => $result->phoneNumber]);
                } catch (PDOException $e) {
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success('获取成功', $result->phoneNumber);
            } else {
                $this->error('获取失败');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

//    /**
//     * 下单之前更新手用户姓名 已废弃
//     * @ApiMethod (GET)
//     * @param $name string 用户姓名
//     */
//    public function updateName()
//    {
//        if ($this->request->isGet()) {
//
//            $name = input('name');
//
//            !$name && $this->error('请传入用户姓名');
//
//            $result = Db::name('user')->where(['id' => $this->auth->id])->update(['name' => $name, 'updatetime' => time()]);
//
//            if ($result) {
//                $this->error('更新成功');
//            } else {
//                $this->error('更新失败');
//            }
//        } else {
//            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
//        }
//    }

    /**
     * 获取用户信息
     * @ApiMethod (GET)
     */
    public function profile()
    {
        if ($this->request->isGet()) {
            $user = $this->auth->getUser();
            $user->visible(['id', 'parent_id', 'name', 'nickname', 'avatar', 'mobile']);
            $user = $user->toArray();

            $user['gymnasium'] = Db::name('gymnasium')
                ->where(['user_id' => $user['id']])
                ->field('id,name,status')
                ->find();

            $user['coach'] = Db::name('coach')
                ->where(['user_id' => $user['id']])
                ->field('id,name,status')
                ->find();

            $user['staff'] = Db::name('staff')
                ->where(['user_id' => $user['id']])
                ->field('id,gymnasium_id,status')
                ->find();

            $user['reward'] = Db::name('extension')
                ->where(['user_id' => $user['id']])
                ->field('id,status,end_time,validity_time,gymnasium_id')
                ->find();
            if ($user['reward']) {
                $find = Db::name('gymnasium')->where(['id' => $user['reward']['gymnasium_id']])->field('name')->find();

                $user['reward']['gymnasium_name'] = $find['name'];
                if ($user['reward']['end_time'] != 0) $user['reward']['end_time'] = date("Y-m-d H:i:s", $user['reward']['end_time']);
                if ($user['reward']['validity_time'] != 0) $user['reward']['validity_time'] = date("Y-m-d H:i:s", $user['reward']['validity_time']);
            }

//            Log::info($user);
            $this->success('获取成功', $user);
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 收藏列表、收藏、取消收藏
     * @param $type int 类型：type 1=场馆,2=教练
     * @param $gymnasium_id int 场馆主键：gymnasium_id
     * @param $coach_id int 教练主键：coach_id
     * @ApiMethod (GET)
     */
    public function like()
    {
        if ($this->request->isGet()) {
            $type         = input('type');
            $gymnasium_id = input('gymnasium_id');
            $coach_id     = input('coach_id');

            $result = null;
            $msg    = '获取成功';
            !$type && $this->error('请传入类型');
//            if ($type == 1 && !$gymnasium_id) $this->error('请传入场馆主键');
//            if ($type == 2 && !$coach_id) $this->error('请传入教练主键');


            if ($type == 1 && $gymnasium_id) {
                $find = Db::name('user_like')->where(['user_id' => $this->auth->id, 'gymnasium_id' => $gymnasium_id])->find();
                if ($find) {
                    $operate = Db::name('user_like')->delete(['id' => $find['id']]);
                    Db::name('gymnasium')->where(['id' => $gymnasium_id])->setDec('collections');
                    $operate > 0 && $msg = '取消收藏';
                } else {
                    $operate = Db::name('user_like')->insert([
                        'user_id'      => $this->auth->id,
                        'gymnasium_id' => $gymnasium_id,
                        'type'         => 1,
                        'createtime'   => time()
                    ]);
                    Db::name('gymnasium')->where(['id' => $gymnasium_id])->setInc('collections');
                    $operate > 0 && $msg = '收藏成功';
                }
            }

            if ($type == 2 && $coach_id) {
                $find = Db::name('user_like')->where(['user_id' => $this->auth->id, 'coach_id' => $coach_id])->find();
                if ($find) {
                    $operate = Db::name('user_like')->delete(['id' => $find['id']]);
                    Db::name('coach')->where(['id' => $coach_id])->setDec('collections');
                    $operate > 0 && $msg = '取消收藏';
                } else {
                    $operate = Db::name('user_like')->insert([
                        'user_id'    => $this->auth->id,
                        'coach_id'   => $coach_id,
                        'type'       => 2,
                        'createtime' => time()
                    ]);
                    Db::name('coach')->where(['id' => $coach_id])->setInc('collections');
                    $operate > 0 && $msg = '收藏成功';
                }
            }

            if ($type == 1 && !$gymnasium_id && !$coach_id) {
                $result = Db::name('user_like')
                    ->alias('like')
                    ->join('gymnasium gymnasium', 'like.gymnasium_id = gymnasium.id', 'left')
                    ->where(['like.user_id' => $this->auth->id, 'like.type' => 1, 'gymnasium.status' => 1])
                    ->field('gymnasium.thumb_image,gymnasium.id,gymnasium.name,gymnasium.address')
                    ->order('like.id', 'desc')
                    ->paginate(null, false, $this->paginate)
                    ->each(function ($item) {
                        $item['thumb_image'] = self::patch_cdn($item['thumb_image']);
                        return $item;
                    });
            }

            if ($type == 2 && !$gymnasium_id && !$coach_id) {
                $result = Db::name('user_like')
                    ->alias('like')
                    ->join('coach coach', 'like.coach_id = coach.id', 'left')
                    ->where(['like.user_id' => $this->auth->id, 'like.type' => 2, 'coach.status' => 1])
                    ->field('coach.portrait_image,coach.id,coach.name,coach.speciality')
                    ->order('like.id', 'desc')
                    ->paginate(null, false, $this->paginate)
                    ->each(function ($item) {
                        $item['portrait_image'] = self::patch_cdn($item['portrait_image']);
                        return $item;
                    });
            }

            $this->success($msg, $result);
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 我的会员卡
     * @ApiMethod (GET)
     */
    public function card()
    {
        $card_status = [
            1 => '未使用',
            2 => '已使用',
            3 => '已到期',
        ];
        $type        = [
            1 => '次卡',
            2 => '私教卡',
            3 => '周卡',
            4 => '月卡',
            5 => '学生卡'
        ];

        if ($this->request->isGet()) {

            $result = Db::name('order')
                ->alias('order')
                ->join('gymnasium gymnasium', 'gymnasium.id = order.gymnasium_id', 'left')
                ->where(['order.user_id' => $this->auth->id, 'order.status' => ['in', '1,2,3']])
                ->field('order.id,order.good_name,order.good_type,order.validity_time,order.thumb_image,order.numbers,order.end_time,order.status,gymnasium.name as gymnasium_name,gymnasium.id as gymnasium_id')
                ->order('order.updatetime', 'desc')
                ->paginate(null, false, $this->paginate)
                ->each(function ($item) use ($card_status, $type) {
                    $item['thumb_image']    = self::patch_cdn($item['thumb_image']);
                    $item['validity_time']  = date("Y-m-d H:i:s", $item['validity_time']);
                    $item['end_time']       = date("Y-m-d H:i:s", $item['end_time']);
                    $item['status_text']    = $card_status[$item['status']];
                    $item['good_type_text'] = $type[$item['good_type']];

                    return $item;
                });
            $this->success('获取成功', $result);
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 训练记录
     * @ApiMethod (GET)
     */
    public function train()
    {
        if ($this->request->isGet()) {
            // 连接缓存
            $redis = new \Redis();
            $redis->pconnect(config('redis.host'), config('redis.port'), config('redis.timeout'));
            $redis->select(5);
            $train = $redis->get($this->auth->id);
            if ($train) {
                $train = unserialize($train);
                unset($train['data']);
                $this->success('获取成功', $train);
            } else {
                $this->error('获取失败');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 系统通知
     * @ApiMethod (GET)
     */
    public function notice()
    {

        if ($this->request->isGet()) {
            $read   = [
                0 => '未读',
                1 => '已读'
            ];
            $result = Db::name('notice')
                ->where(['user_id' => $this->auth->id])
                ->order('createtime', 'desc')
                ->field('order_numbers,content,read,createtime')
                ->paginate(null, false, $this->paginate)
                ->each(function ($item) use ($read) {
                    $item['createtime'] = date("Y-m-d H:i:s", $item['createtime']);
                    $item['read_text']  = $read[$item['read']];
                    return $item;
                });
            $this->success('获取成功', $result);
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

//    /**
//     * 推广记录 已废弃
//     * @ApiMethod (GET)
//     */
//    public function extension()
//    {
//        if ($this->request->isGet()) {
//            $type   = [
//                1 => '周卡',
//                2 => '三日卡',
//                3 => '教练卡',
//            ];
//            $is_use = [
//                0 => '不可用',
//                1 => '可用'
//            ];
//            $status = [
//                0 => '未使用',
//                2 => '已使用',
//                3 => '已到期'
//            ];
//            $result = Db::name('extension')
//                ->alias('extension')
//                ->where(['extension.user_id' => $this->auth->id])
//                ->join('gymnasium gymnasium', 'gymnasium.id = extension.gymnasium_id', 'left')
//                ->field('extension.id,extension.type,extension.is_use,extension.gymnasium_id,gymnasium.name as gymnasium_name,extension.status,extension.use_time,extension.end_number,extension.end_time,extension.validity_time,extension.createtime')
//                ->paginate(null, false, $this->paginate)
//                ->each(function ($item) use ($type, $is_use, $status) {
//                    $item['createtime']    = date("Y-m-d H:i:s", $item['createtime']);
//                    $item['use_time']      = date("Y-m-d H:i:s", $item['use_time']);
//                    $item['end_time']      = date("Y-m-d H:i:s", $item['end_time']);
//                    $item['validity_time'] = date("Y-m-d H:i:s", $item['validity_time']);
//                    $item['type_text']     = $type[$item['type']];
//                    $item['is_use_text']   = $is_use[$item['is_use']];
//                    $item['status_text']   = $status[$item['status']];
//                    return $item;
//                });
//            $this->success('获取成功', $result);
//        } else {
//            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
//        }
//    }

    /**
     * 新用户主动获取三日卡 2020-09-29
     * 新用户判断依据 extension表是否存在该用户的三日卡
     * @ApiMethod (GET)
     */
    public function newUser()
    {
        !$this->request->isGet() && $this->error(ErrorCode::$ERROR['ERROR_METHOD']);

        // 判断是否是新用户
        $is_new = Db::name('extension')->where(['user_id' => $this->auth->id])->field('id')->find();
        if ($is_new) {
            $this->error('您不是新用户,并且已经领取过三日卡');
        } else {
            $redis = new \Redis();
            $redis->pconnect(config('redis.host'), config('redis.port'), config('redis.timeout'));
            $extension = [
                'user_id'       => $this->auth->id,
                'validity_time' => time() + 3600 * 24 * 9,
                'createtime'    => time(),
                'updatetime'    => time(),
            ];
            Db::startTrans();
            try {
                $extension_id = Db::name('extension')->insertGetId($extension);
                $redis->select(6);
                $redis->setex($extension_id, 3600 * 24 * 9, json_encode($extension, JSON_UNESCAPED_UNICODE));

                $redis->select(9);
                $redis->setex($extension_id, 3600 * 24 * 2, json_encode($extension, JSON_UNESCAPED_UNICODE));

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('领取成功');
        }
    }

    /**
     * 好友助力 2020-09-29
     * 只有授权手机号的用户才能助力
     * @ApiMethod (GET)
     */
    public function friendHelp()
    {
        !$this->request->isGet() && $this->error(ErrorCode::$ERROR['ERROR_METHOD']);

        $parent = input('parent');
        !$parent && $this->error('parent参数无效');
        if ($parent == $this->auth->id) $this->error('您不能给自己助力');

        $find = Db::name('user')->where(['id' => $this->auth->id])->field('id,parent_id,extension_')->find();
        if ($find['extension_'] == 1) $this->error($parent != $find['parent_id'] ? "您已经为其他好友助力过" : "您已经为该好友助力过");

        $find_parent = Db::name('user')->where(['id' => $parent])->field('id,extension_times')->find();
        if (!$find_parent) $this->error('无法获取邀请人');

        $msg  = '助力成功';
        $time = time();

        Db::startTrans();
        try {
            // 被助力人增加助力次数
            Db::name('user')->where(['id' => $parent])->setInc('extension_times');
            // 助力人调整为已助力 调整上级邀请人
            Db::name('user')->where(['id' => $this->auth->id])->update(['extension_' => 1, 'parent_id' => $parent, 'extension_time' => $time]);

            // 三人优惠
            if ($find_parent['extension_times'] == 2) {
                Db::name('extension_001')->insertGetId([
                    'user_id'    => $find_parent['id'],
                    'type'       => 1,
                    'end_time'   => $time + 3600 * 24 * 21,
                    'createtime' => $time,
                    'updatetime' => $time,
                ]);
                $msg = '助力成功,好友已获得99元周卡优惠券一张';
            }

            // 七人优惠
            if ($find_parent['extension_times'] == 6) {
                Db::name('extension_001')->insertGetId([
                    'user_id'    => $find_parent['id'],
                    'type'       => 2,
                    'end_time'   => $time + 3600 * 24 * 90,
                    'createtime' => $time,
                    'updatetime' => $time,
                ]);
                $msg = '助力成功,好友已获得300元月卡优惠券一张';
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success($msg);
    }

    /**
     * 好友助力记录 2020-09-30
     * @ApiMethod (GET)
     */
    public function FriendHelpRecord()
    {
        !$this->request->isGet() && $this->error(ErrorCode::$ERROR['ERROR_METHOD']);

        $parent_id = input('parent_id', null);
        if ($parent_id == null) {
            $parent_id = $this->auth->id;
        }
        $result = Db::name('user')
            ->where(['parent_id' => $parent_id, 'extension_' => 1])
            ->field('id,parent_id,nickname,avatar,extension_time')
            ->limit(7)
            ->select();
        if (count($result) > 0) {
            foreach ($result as &$item) {
                $item['extension_time'] = date('Y-m-d');
            }
            $this->success('获取成功', $result);
        } else {
            $this->error('暂时还没有邀请好友助力');
        }
    }

    /**
     * 获取优惠券 2020-10-05
     * @ApiMethod (GET)
     */
    public function getDiscount()
    {
        !$this->request->isGet() && $this->error(ErrorCode::$ERROR['ERROR_METHOD']);

        $result = Db::name('extension_001')
            ->where(['user_id' => $this->auth->id])
            ->select();

        if (count($result) > 0) {
            foreach ($result as &$item) {
                $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
            }
            $this->success('获取成功', $result);
        } else {
            $this->error('获取失败,[优惠券还未获取或已使用]');
        }
    }

    /**
     * 假数据生成 可恢复 2020-10-21
     */
    public function jia()
    {
        // 每次请求 随机自增 1-10 并记录
        $value = Db::name('gymnasium')->order('id')->column('id,collections,likes,jia_shoucang,jia_dianzan');
        $rand  = array_rand($value, 20);
        $inc   = rand(1, 20);
        $inc_  = rand(1, 20);
        Db::startTrans();
        try {
            foreach ($rand as $item) {
                $update = [
                    'collections'  => $value[$item]['collections'] + $inc_,
                    'likes'        => $value[$item]['likes'] + $inc,
                    'jia_shoucang' => $value[$item]['jia_shoucang'] + $inc_,
                    'jia_dianzan'  => $value[$item]['jia_dianzan'] + $inc,
                ];
                var_dump(Db::name('gymnasium')->where(['id' => $value[$item]['id']])->update($update));
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error('错误信息：' . $e->getMessage());
        }
    }
}
